package com.infosys.newSimpleInvertedIndex.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.infosys.newSimpleInvertedIndex.dto.ResumeDTO;
import com.infosys.newSimpleInvertedIndex.entities.Resume;
import com.infosys.newSimpleInvertedIndex.helper.FileHelper;
import com.infosys.newSimpleInvertedIndex.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tester
 * @since 2024-06-24
 */
@Controller
@RestController
@RequestMapping("/resume")
public class ResumeController {
    // controller
    @Autowired
    private IResumeService resumeService;
    // crud
    // create by user id
    @PostMapping("/addResume")
    public ResponseEntity<ResumeDTO> addResume(String employeeId, MultipartFile resumeFile){
        // upload the file
        String address = null;
        try {
            address = FileHelper.uploadFile(resumeFile, "resume");
        } catch (Exception ioe) {
            ioe.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
        Resume resume = new Resume(employeeId, address);
        boolean result = resumeService.save(resume);
        if (result) {
            ResumeDTO resumeDTO = new ResumeDTO(resume);
            return ResponseEntity.ok(resumeDTO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }
    // update by user id
    @PutMapping("/updateResume")
    public ResponseEntity<ResumeDTO> updateResume(String employeeId, MultipartFile resumeFile) {
        // upload the file
        String address = null;
        try {
            address = FileHelper.uploadFile(resumeFile, "resume");
        } catch (Exception ioe) {
            ioe.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id", employeeId);
        Resume resume = resumeService.getOne(queryWrapper);
        resume.setResumeUrl(address);
        boolean result = resumeService.updateById(resume);
        if (result) {
            ResumeDTO resumeDTO = new ResumeDTO(resume);
            return ResponseEntity.ok(resumeDTO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }
    // delete by user id
    @DeleteMapping("/deleteResume")
    public ResponseEntity<String> deleteResume(String employeeId) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id", employeeId);
        boolean result = resumeService.remove(queryWrapper);
        if (result) {
            return ResponseEntity.ok("Delete successfully");
        } else {
            return ResponseEntity.badRequest().build();
        }
    }
    // get by user id
    @GetMapping("/getByEmployeeId")
    public ResponseEntity<ResumeDTO> getResumeByEmployeeId(String employeeId) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id", employeeId);
        Resume resume = resumeService.getOne(queryWrapper);
        ResumeDTO resumeDTO = new ResumeDTO(resume);
        return ResponseEntity.ok(resumeDTO);
    }
}
